package com.xifly.datatransmission.web;

import com.xifly.datatransmission.SystemProperties;
import com.xifly.datatransmission.domain.Result;
import com.xifly.datatransmission.domain.User;
import com.xifly.datatransmission.domain.UserModel;
import com.xifly.datatransmission.enums.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseController{

    @Autowired
    private SystemProperties systemProperties;

    @Value("${user.username}")
    private String username;
    @Value("${user.password}")
    private String password;

    @RequestMapping("/login")
    public Result login(@RequestBody @Valid UserModel userModel){

        if (!username.equals(userModel.getUsername()) || !password.equals(userModel.getPassword())){
            return new Result(ResultCode.PASSWORD_ERROR);
        }
        User user = new User();
        user.setUsername(userModel.getUsername());
        user.setPassword(userModel.getPassword());
        return new Result(ResultCode.SUCCESS,user);
    }

    @RequestMapping("/list")
    public Result list() {

        if (systemProperties.getUsers() != null){
            throw new RuntimeException();
        }
        return new Result(ResultCode.SUCCESS, systemProperties.getUsers());
    }
}
